﻿using System;
using System.Collections.Generic;
using System.Text;
using Portal.Entity;
using Portal.Interface;
using MyLib;

namespace Portal.Logic
{
    /// <summary>
    /// 资源访问类，获取所有资源请用此类
    /// </summary>
    public class ResourceLogic
    {
        public List<ResourceEntity> GetResourceList()
        {
            IResource[] resources = ServiceContainer.Instance().GetAll<IResource>();
            List<ResourceEntity> lists = new List<ResourceEntity>();
            foreach (IResource resource in resources)
            {
                lists.AddRange(resource.GetResourceList());
            }
            return lists;
        }

        public void ClearResourceList()
        {
            IResource[] resources = ServiceContainer.Instance().GetAll<IResource>();
            foreach (IResource resource in resources)
            {
                resource.ClearResourceList();
            }
        }

        public ResourceEntity GetEntity(int resourceId)
        {
            List<ResourceEntity> list = GetResourceList();
            return list.Find(p => p.RESOURCEID == resourceId);
        }

        /// <summary>
        /// 获取系统配置中的值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string GetKeyValue(string key)
        {
            DBResourceLogic logic = new DBResourceLogic();
            List<ResourceEntity> list = logic.GetResourceList();
            ResourceEntity resource= list.Find(p => p.EXT01 == key && p.TYPE == "Settings");
            if (resource != null)
            {
                return resource.EXT02;
            }

            return string.Empty;
        }

        public List<ResourceEntity> GetResourceList(string type)
        {
            ResourceLogic resource = new ResourceLogic();
            List<ResourceEntity> list = resource.GetResourceList();
            return list.FindAll(p => p.TYPE == type);
        }

        public void Save(ResourceEntity ety)
        {
            int count = DataAccess.Instance("BizDB").GetEntity<int>("ResourceLogic_Exist", ety.RESOURCEID);
            //保存
            if (count > 0)
            {
                DataAccess.Instance("BizDB").Update("ResourceLogic_Update", ety);
            }
            else
            {
                DataAccess.Instance("BizDB").Insert("ResourceLogic_Insert", ety);
            }
        }

        public void DeleteResource(int resouceId)
        {
            string strSql = "DELETE FROM COM_RESOURCE WHERE RESOURCEID=" + resouceId;
            DataAccess.Instance("BizDB").ExecuteScalar(strSql);
            strSql="DELETE FROM COM_RESOURCERELATION WHERE SOURCEID=" + resouceId;
            DataAccess.Instance("BizDB").ExecuteScalar(strSql);
        }

        public List<string> GetTopTypeList()
        {
            return DataAccess.Instance("BizDB").GetList<string>("select distinct type from COM_RESOURCE where parentid=0 or parentid is null");

        }
    }
}
